package cn.jeeweb.web.modules.equipment.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.equipment.entity.EqsEquipment;
import cn.jeeweb.web.modules.equipment.entity.EqsEquipmentFixture;
import cn.jeeweb.web.modules.equipment.entity.EqsSparePart;
import cn.jeeweb.web.modules.equipment.service.IEqsEquipmentFixtureService;
import cn.jeeweb.web.modules.equipment.service.IEqsEquipmentService;
import cn.jeeweb.web.modules.equipment.service.IEqsSparePartService;
import cn.xjzn.framework_Core.ModuleActionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/eqs/equipmentfixture")
@ViewPrefix("")
@Log(title = "eqs_equipment_fixture")
public class EqsEquipmentFixtureController {

    @Autowired
    private IEqsEquipmentFixtureService eqsEquipmentFixtureService;
    @Autowired
    private IEqsEquipmentService eqsEquipmentService;
    @Autowired
    private IEqsSparePartService eqsSparePartService;

    @PostMapping("save")
    @Log(logType = ModuleActionType.INSERT)
    public Response save(String fixtureId, String[] equipmentIds) {
//        if (StringUtils.isNotBlank(fixtureId)) {
//            EntityWrapper<EqsEquipmentFixture> entityWrapper = new EntityWrapper<>(EqsEquipmentFixture.class);
//            entityWrapper.eq("fixtureId", fixtureId);
//            eqsEquipmentFixtureService.delete(entityWrapper);
//        } else {
//            return Response.error("数据格式有误");
//        }

        if (equipmentIds != null && equipmentIds.length > 0) {
            for (String equipmentId : equipmentIds) {
                EqsEquipmentFixture eqsEquipmentFixture = new EqsEquipmentFixture();
                eqsEquipmentFixture.setFixtureId(fixtureId);
                eqsEquipmentFixture.setEquipmentId(equipmentId);
                eqsEquipmentFixtureService.insert(eqsEquipmentFixture);
            }
        }
        return Response.ok("保存成功");
    }

    /**
     * 获取工装所属的设备
     * @param fixtureId
     * @return
     */
    @PostMapping("getEquipmentIds")
    public Response getEquipmentIds(String fixtureId) {
        Map map = new HashMap<String, Object>();
        EntityWrapper<EqsEquipmentFixture> entityWrapper = new EntityWrapper<>(EqsEquipmentFixture.class);
        entityWrapper.eq("fixtureId", fixtureId);
        List<EqsEquipmentFixture> list = eqsEquipmentFixtureService.selectList(entityWrapper);
        if (list != null && list.size() > 0) {
            String[] array = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                array[i] = list.get(i).getEquipmentId();
            }
            map.put("record", array);
        }
        return Response.ok("", map);
    }

    /**
     * 删除工装与设备的关联
     * @param templateIds
     * @return
     */
    @RequestMapping(value = "batchDelete", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam(value = "id", required = false)String id,@RequestParam(value = "templateIds", required = false)String[] templateIds) {
        EntityWrapper<EqsEquipmentFixture> entityWrapper = new EntityWrapper<>(EqsEquipmentFixture.class);
        entityWrapper.eq("fixtureId",id);
        entityWrapper.in("equipmentId",templateIds);
        eqsEquipmentFixtureService.delete(entityWrapper);
        return Response.ok("删除成功");
    }

    @RequestMapping(value = "getEqsEquipmentFixtures", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public Response getEqsEquipmentFixtures(String fixtureId){
        List<EqsEquipment> eqsEquipments = eqsEquipmentService.getEqsEquipmentByFixtureId(fixtureId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("records",eqsEquipments);
        return Response.ok("",map);
    }

    @RequestMapping(value = "getSparePartByEquipmentId", method = {RequestMethod.GET,RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public Response getSparePartByEquipmentId(String equipmentId){
        List<EqsSparePart> eqsSpareParts = eqsSparePartService.getSparePartByEquipmentId(equipmentId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("records",eqsSpareParts);
        return Response.ok("",map);
    }


}